package script.login
{
	import eventUtil.EventCenter;
	
	import laya.components.Script;
	import laya.display.Input;
	import laya.events.Event;
	import laya.maths.Point;
	import laya.net.Loader;
	import laya.ui.List;
	import laya.utils.Browser;
	import laya.utils.Handler;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	
	import script.ViewManager;
	
	import ui.login.RegisterPanelUI;
	
	import utils.UtilTool;
	import utils.WaitingRespond;
	
	public class RegisterCntrol extends Script
	{
		private var uiSkin:RegisterPanelUI;
		
		
		private var verifycode:Object;
		public var param:Object;

		private var phonecode:String = "";
		
		private var coutdown:int = 60;
		
		private var dateInput:Object;
	

		public function RegisterCntrol()
		{
			super();
		}
		
		override public function onStart():void
		{
			
			uiSkin = this.owner as RegisterPanelUI; 
						
			uiSkin.mainpanel.vScrollBarSkin = "";
			uiSkin.mainpanel.hScrollBarSkin = "";

//			uiSkin.input_adress.maxChars = 200;
//			uiSkin.input_company.maxChars = 50;
			uiSkin.input_conpwd.maxChars = 20;
			uiSkin.input_conpwd.type = Input.TYPE_PASSWORD;
			uiSkin.input_phone.maxChars = 11;
			uiSkin.input_phone.restrict = "0-9";
			uiSkin.input_phonecode.maxChars = 6;
			
			uiSkin.input_pwd.maxChars = 20;
			uiSkin.input_pwd.type = Input.TYPE_PASSWORD;
			
			uiSkin.nameInput.maxChars = 20;
			


			uiSkin.btnGetCode.on(Event.CLICK,this,onGetPhoneCode);


			uiSkin.txtRefresh.on(Event.CLICK,this,onRefreshVerify);

			uiSkin.btnReg.on(Event.CLICK,this,onRegister);
		//	uiSkin.mainpanel.height = Browser.height;
		//	uiSkin.mainpanel.width = Browser.width;

			
			//EventCenter.instance.on(EventCenter.BROWER_WINDOW_RESIZE,this,onResizeBrower);
			uiSkin.backbtn.on(Event.CLICK,this,function(){
				
				ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);
			});
			var years:Array = [];
			for(var i:int=1980;i < 2021;i++)
				years.push(i);
			
			var months:Array = [];
			for(var i:int=1;i < 13;i++)
				months.push(i);
			
			var days:Array = [];
			for(var i:int=1;i < 32;i++)
				days.push(i);
		

		}
		

		private function initDateSelector():void
		{
			var curdate:Date = new Date((new Date()).getTime() -  24 * 3600 * 1000);
			
			var lastdate:Date = new Date();
			
			//trace(UtilTool.formatFullDateTime(curdate,false));
			//trace(UtilTool.formatFullDateTime(nextmonth,false));
			
			//var curyear:int = (new Date()).getFullYear();
			//var curmonth:int = (new Date()).getMonth();
			
			
			dateInput = Browser.document.createElement("input");
			
			dateInput.style="filter:alpha(opacity=100);opacity:100;left:795px;top:240";
			
			dateInput.style.width = 150;
			dateInput.style.height = 30;
			
			//			if(param && param.type == "License")
			//				file.multiple="";
			//			else			
			
			dateInput.type ="date";
			dateInput.style.position ="absolute";
			dateInput.style.zIndex = 999;
			dateInput.value = UtilTool.formatFullDateTime(curdate,false);
			Browser.document.body.appendChild(dateInput);//添加到舞台
			
			dateInput.onchange = function(datestr):void
			{
				if(dateInput.value == "")
					return;
				var curdata:Date = new Date(dateInput.value);
				
				
			}
			
			
		}
		
		private function updateDateInputPos():void
		{
			if(dateInput != null)
			{
				//verifycode.style.top = 548 - uiSkin.mainpanel.vScrollBar.value + "px";
				
			}
			
		}
		
		private function initCode():void
		{
			
			verifycode = Browser.document.createElement("div");
			verifycode.id = "v_container";
			verifycode.style="width: 200px;height: 50px;left:950px;top:548";
			verifycode.style.width = 200/Browser.pixelRatio + "px";
			verifycode.style.height = 50/Browser.pixelRatio + "px";
			
			verifycode.style.left = (950 - (1920 - Browser.width)/2) + "px";
			
			verifycode.style.position ="absolute";
			verifycode.style.zIndex = 999;
			Browser.document.body.appendChild(verifycode);//添加到舞台
			
			//uiSkin.on(Event.CLICK,this,hideAddressPanel);
			Browser.window.loadVerifyCode();
			
		}
		
		
		private function updateVerifyPos():void
		{
			if(verifycode != null)
			{
				//verifycode.style.top = 548 - uiSkin.mainpanel.vScrollBar.value + "px";
				var pt:Point = uiSkin.inputCode.localToGlobal(new Point(uiSkin.inputCode.x,uiSkin.inputCode.y),true);
				var offset:Number = 0;
				if(Browser.width > Laya.stage.width)
					offset = (Browser.width - Laya.stage.width)/2;
				
				verifycode.style.top = pt.y/Browser.pixelRatio + "px";
				verifycode.style.left = (pt.x +  80 + offset)/Browser.pixelRatio +  "px";
				
				verifycode.style.width = 200/Browser.pixelRatio + "px";
				verifycode.style.height = 50/Browser.pixelRatio + "px";

				//trace("pos:" + pt.x + "," + pt.y);
				//verifycode.style.left = 950 -  uiSkin.mainpanel.hScrollBar.value + "px";

			}
			
		}
		private function onGetPhoneCode():void
		{
			// TODO Auto Generated method stub
			if(uiSkin.input_phone.text.length < 11)
			{
				Browser.window.alert("请填写正确的手机号");
				return;
			}
			uiSkin.btnGetCode.disabled = true;
			
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getVerifyCode ,this,onGetPhoneCodeBack,"phonenum=" + uiSkin.input_phone.text,"post");
		}
		
		private function onGetPhoneCodeBack(data:String):void
		{
			var result:Object = JSON.parse(data);
			if(result.code == HttpErrorCode.OK)
			{
				phonecode = result.code;
				uiSkin.btnGetCode.label = "60秒后重试";
				Laya.timer.loop(1000,this,countdownCode);
			}
			else
			{
				uiSkin.btnGetCode.disabled = false;
				ViewManager.showAlert(result.msg);
			}

		}
		
		private function countdownCode():void
		{
			coutdown--;
			if(coutdown > 0)
			{
				uiSkin.btnGetCode.label = coutdown + "秒后重试";
			}
			else
			{
				uiSkin.btnGetCode.disabled = false;
				uiSkin.btnGetCode.label = "获取验证码";
				Laya.timer.clear(this,countdownCode);
			}
		}
		private function onRefreshVerify(e:Event):void
		{
			Browser.window.loadVerifyCode();

		}
		private function onCloseScene():void
		{
			Browser.document.body.removeChild(verifycode);//添加到舞台
			Laya.timer.clear(this,countdownCode);

			ViewManager.instance.closeView(ViewManager.VIEW_REGISTER_PANEL);
			ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);

			//this.owner.removeSelf();
		}		
		
		private function onRegister():void
		{
			//if(uiSkin.
			var res:Boolean = true;//Browser.window.checkCode(uiSkin.inputCode.text);
			if(res)
			{
				if(uiSkin.input_phone.text.length < 11)
				{
					Browser.window.alert("请填写正确的手机号");
					return;
				}
//				if(uiSkin.nameInput.text.length == 0)
//				{
//					Browser.window.alert("请输入用户名");
//					return;
//				}
				
				if(uiSkin.input_pwd.text.length < 6)
				{
					Browser.window.alert("密码长度至少6位");
					return;
				}
				if(uiSkin.input_pwd.text != uiSkin.input_conpwd.text)
				{
					Browser.window.alert("密码确认不对");
					return;
				}
				
//				if(uiSkin.yearsel.selectedLabel == null || uiSkin.monthsel.selectedLabel == null || uiSkin.daysel.selectedLabel == null)
//				{
//					Browser.window.alert("密码确认不对");
//					return;
//				}

				var param:String = "urPhone=" + uiSkin.input_phone.text + "&urName=" + uiSkin.nameInput.text + "&urPwd=" + uiSkin.input_pwd.text;
				HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.registerUrl,this,onRegisterBack,param,"post");

			}
			else
			{
				Browser.window.alert("验证码错误");
				//Browser.window.loadVerifyCode();

			}

		}
		
		private function onRegisterBack(data:String):void
		{
			// TODO Auto Generated method stub
			var result:Object = JSON.parse(data);
			if(result.code == HttpErrorCode.OK)
			{
				Browser.window.alert("注册成功！");
				//Browser.document.body.removeChild(verifycode);//添加到舞台

				//ViewManager.instance.openView(ViewManager.VIEW_FIRST_PAGE,true);

				ViewManager.instance.openView(ViewManager.VIEW_LOGIN_PANEL,true);

			}
			else
				ViewManager.showAlert(result.msg);
		}		
		public override function onDestroy():void
		{
			
			Laya.timer.clearAll(this);

		}

		
	}
}